<?php
/**
 * @package			ALM ProductControl
 * @version			$Id:$
 * @subpackage		com_almapc
 * @autor			ALM Studio
 * @copyright		Copyrights ALM Studio 2011. All rights reserved
 * @autor url		http://alm-studio.net
 * @autor email		info@almstudio.net
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class ALMPCModelItems extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'ItemId', 'a.ItemId',
				'ItemCategory', 'a.ItemCategory',
				'ItemType', 'a.ItemType',
				'ItemNotes', 'a.ItemNotes',
				'ItemThickness', 'a.ItemThickness',
				'ItemWidth', 'a.ItemWidth',
				'ItemLength', 'a.ItemLength',
				'ItemQuality', 'a.ItemQuality',
				'ItemThickness', 'a.ItemWidth',
				'ItemWeight', 'a.ItemWeight',
				'ItemPrice', 'a.ItemPrice',
				'ItemModifiedBy', 'a.ItemModifiedBy',
				'ItemStatus', 'a.ItemStatus'
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		
		$categoryId = $app->getUserStateFromRequest($this->context.'.filter.category_id', 'filter_category_id');
		$this->setState('filter.category_id', $categoryId);
		
		$typeId = $app->getUserStateFromRequest($this->context.'.filter.type_id', 'filter_type_id');
		$this->setState('filter.type_id', $typeId);
		
		$statusId = $app->getUserStateFromRequest($this->context.'.filter.status_id', 'filter_status_id');
		$this->setState('filter.status_id', $statusId);
		
		parent::populateState('a.ItemId', 'desc');		
				
	}
	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select($this->getState('list.select', 'a.*,(a.ItemWeight*a.ItemPrice) AS ItemCost'));
		$query->from('#__almpc_items AS a');
		
		$categoryId = $this->getState('filter.category_id');
		if (is_numeric($categoryId)) {
			$query->where('a.ItemCategory = '.$categoryId);
		}
		
		$statusId = $this->getState('filter.status_id');
		if (is_numeric($statusId)) {
			$query->where('a.ItemStatus = '.$statusId);
		}
		
		$typeId = $this->getState('filter.type_id');
		if (is_numeric($typeId)) {
			$query->where('a.ItemType = '.$typeId);
		}
		
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'CatName:') === 0) {
				$query->where('a.CatName = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(a.CatName LIKE '.$search.')');
			}
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.ItemId')).' '.$db->getEscaped($this->getState('list.direction', 'desc')));
			
		return $query;		
	}
}